package com.cortex.papayasearch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.carrot2.core.Document;
import org.carrot2.source.google.GoogleDocumentSource;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.cortex.papaya.activities.MainActivity;
import com.cortex.papaya.search.FetchIcons;
import com.cortex.papaya.search.FetchIcons.OnStateIconsChanged;
import com.cortex.papaya.search.Categories;
import com.cortex.papaya.search.FetchData;
import com.cortex.papaya.search.Categories.OnStateCategoriesChangedListener;
import com.cortex.papaya.search.Link;
import com.cortex.papaya.search.FetchData.OnStateDataChanged;
import com.cortex.papaya.search.Category;
import com.cortex.papaya.search.QuerySearch;
import com.cortex.papaya.utilities.MyLog;
import com.cortex.papaya.view.PapayaHeaderView;
import com.cortex.papaya.view.PapayaHelpView;

public class PapayaApplication extends Application 
	implements OnStateDataChanged, OnStateIconsChanged, OnStateCategoriesChangedListener {

	private Categories categories;
	private FetchData fetchData;
	private FetchIcons fetchIcons;
	private HelpData helpData;
	private OnSearchDataStateChanged onSearchDataStateChanged;
	private OnSearchIconsStateChanged onSearchIconsStateChanged;

	public PapayaApplication(){
		fetchData = new FetchData();
		fetchIcons = new FetchIcons();
		helpData = new HelpData();
		categories = new Categories();
		initListener();
	}
	private void initListener(){
		fetchData.setDataLoadedListener(this);
		fetchIcons.setDataLoadedListener(this);
		categories.setOnStateCategoriesChangedListener(this);
	}
	public HelpData getHelpData(){
		return helpData;
	}
	public Categories getCategories() {
		return categories;
	}
	public void setCategories(Categories categories) {
		this.categories = categories;
	}
	public void setOnSearchDataStateChangedListener(OnSearchDataStateChanged onSearchDataStateChanged) {
		this.onSearchDataStateChanged = onSearchDataStateChanged;
	}
	public void setOnSearchIconsStateChangedListener(OnSearchIconsStateChanged onSearchIconsStateChanged) {
		this.onSearchIconsStateChanged = onSearchIconsStateChanged;
	}
	public boolean isDataSearchRunning(){
		if(fetchData.isFetchRunning()){
			return true;
		}
		return false;
	}
	public boolean isIconsSearchRunning(){
		if(fetchIcons.isFetchRunning()){
			return true;
		}
		return false;
	}
	public boolean isSearchRunning(){
		if(fetchData.isFetchRunning() || fetchIcons.isFetchRunning()){
			return true;
		}
		return false;
	}
	public void cancelSearchData(){
		if(fetchData != null){
			if(fetchData.isFetchRunning()){
				fetchData.cancelFetching();
			}
		}
	}
	public void cancelSearchIcons(){
		if(fetchIcons != null){
			fetchIcons.cancelFetching();
		}
	}
	public void cancelAllSearch(){
		cancelSearchData();
		cancelSearchIcons();
	}
	public void runSearchData(String query){
		QuerySearch querySearch = new QuerySearch(new GoogleDocumentSource(),query);
		fetchData.setQuerySearch(querySearch);
		fetchData.startFetching();
	}
	public void runSearchIcons(final Category category){
		fetchIcons.startFetching(category.getLinks());
	}
	public boolean isInternetConnection(){
		ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		return networkInfo == null?false:networkInfo.isConnected();

	}
	public interface OnSearchDataStateChanged{
		void startedSearchData();
		void finishedSearchData(Categories categories);
		void canceledSearchData(String message);
	}
	public interface OnSearchIconsStateChanged{
		void startedSearchIcons();
		void finishedSearchIcons(Category category);
		void canceledSearchIcons();
	}
	public void saveData(String key,boolean Value){          
		Context context = getApplicationContext();
		SharedPreferences prefsDefault = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = prefsDefault.edit();
		editor.putBoolean(key, true);
		editor.commit();
	}
	public boolean reloadData(String key){  
		Context context = getApplicationContext();
		SharedPreferences prefsDefault = PreferenceManager.getDefaultSharedPreferences(context);
		return prefsDefault.getBoolean(key, false);
	}

	public ScreenSize getScreenSize(Activity activity){
		Rect rect = new Rect();
		Window win = activity.getWindow();  // Get the Window
		win.getDecorView().getWindowVisibleDisplayFrame(rect);
		DisplayMetrics metrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);   
		int screenHeight = metrics.heightPixels;
		int screenWidth = metrics.widthPixels ;
		return new ScreenSize(screenWidth, screenHeight);
	}
	public class ScreenSize{

		private int width,height;

		public ScreenSize(int width,int height) {
			this.width = width;
			this.height = height;
		}

		public int getWidth() {
			return width;
		}
		public int getHeight() {
			return height;
		}
	}
	// listeners of icons search
	@Override
	public void timeLoadIconsStarted() {
		if(onSearchIconsStateChanged != null){
			onSearchIconsStateChanged.startedSearchIcons();
		}
	}
	
	
	@Override
	public void timeLoadIconsFinished(List<Link> documents) {
		if(onSearchIconsStateChanged != null){
			onSearchIconsStateChanged.finishedSearchIcons(categories.getCategories().get(0));
		}
	}

	@Override
	public void timeLoadIconsCanceled() {
		if(onSearchIconsStateChanged != null){
			onSearchIconsStateChanged.canceledSearchIcons();
		}
	}
	// listeners for data search
	@Override
	public void timeLoadDataStarted() {
		if(onSearchDataStateChanged != null){
			onSearchDataStateChanged.startedSearchData();
		}
	}
	@Override
	public void timeLoadDataFinished(List<Document> documents) {
		List<Link> documents1 = new ArrayList<Link>();
		for (Document document : documents) {
			documents1.add(new Link(document));
		}
		for (Link papayaDocument : documents1) {
			Log.d(MyLog.PAPAYA_LOG,papayaDocument.toString());
			Log.d(MyLog.PAPAYA_LOG,"////////////////////////////////");
		}
		categories.runBuildCategories(documents, fetchData.getQuerySearch().getQuery());
	}
	@Override
	public void timeLoadDataCanceled(String message) {
		if(onSearchDataStateChanged != null){
			onSearchDataStateChanged.canceledSearchData(message);
		}
	}
	
	// listeners for build categories 
	@Override
	public void timeLoadBuiltStarted() {
		
	}

	@Override
	public void timeLoadBuiltFinished() {
		if(onSearchDataStateChanged != null){
			onSearchDataStateChanged.finishedSearchData(categories);
		}
	}

	@Override
	public void timeLoadBuiltCanceled(String message) {
		
	}

}
